package com.chj.comp.common.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chj.comp.common.auth.entity.SysUser;
import com.chj.comp.common.auth.mapper.SysUserMapper;
import com.chj.comp.common.auth.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cuihui
 * @since 2022-02-15
 */
@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Override
    public SysUser getUserByUsername(String username) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public SysUser getUserByPhone(String phone) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        SysUser sysUser = this.getOne(wrapper.eq(SysUser::getPhone, phone));
        if(sysUser == null){
            // 新增手机号用户,默认密码123
            sysUser = SysUser.builder()
                    .username(phone)
                    .password(new BCryptPasswordEncoder().encode("123"))
                    .fullname(phone)
                    .phone(phone)
                    .bz("默认密码：123")
                    .build();
            this.save(sysUser);
        }
        return sysUser;
    }

}
